package com.hy.controller;

import com.hy.Result.ResponseResult;

import com.hy.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.hy.medicine.UserAddress;
import java.util.List;

/**
 * 地址管理控制器
 */
@RestController
@RequestMapping("/api/address")
public class AddressController {

    @Autowired
    private UserAddressService userAddressService;

    /**
     * 获取用户的所有地址
     * @param userId 用户ID
     * @return 地址列表
     */
    @GetMapping("/list")
    public ResponseResult getAddressList(@RequestParam("userId") Long userId) {

        return userAddressService.getAddressByUserId(userId);
    }

    /**
     * 获取地址详情
     * @param addressId 地址ID
     * @return 地址详情
     */
    @GetMapping("/{addressId}")
    public ResponseResult<UserAddress> getAddress(@PathVariable("addressId") Long addressId) {
        UserAddress address = userAddressService.getAddressById(addressId);
        if (address == null) {
            return ResponseResult.errorResult(404, "地址不存在");
        }
        return ResponseResult.okResult(address);
    }

    /**
     * 获取用户的默认地址
     * @param userId 用户ID
     * @return 默认地址
     */
    @GetMapping("/default")
    public ResponseResult<UserAddress> getDefaultAddress(@RequestParam("userId") Long userId) {
        UserAddress address = userAddressService.getDefaultAddress(userId);
        if (address == null) {
            return ResponseResult.errorResult(404, "未设置默认地址");
        }
        return ResponseResult.okResult(address);
    }

    /**
     * 添加新地址
     * @param address 地址信息
     * @return 新增的地址ID
     */
    @PostMapping("/add")
    public ResponseResult<Long> addAddress(@RequestBody UserAddress address) {
        try {
            Long addressId = userAddressService.addAddress(address);
            return ResponseResult.okResult(addressId);
        } catch (IllegalArgumentException e) {
            return ResponseResult.errorResult(400, e.getMessage());
        } catch (Exception e) {
            return ResponseResult.errorResult(500, "添加地址失败：" + e.getMessage());
        }
    }

    /**
     * 更新地址
     * @param address 地址信息
     * @return 操作结果
     */
    @PutMapping("/update")
    public ResponseResult<Boolean> updateAddress(@RequestBody UserAddress address) {
        try {
            boolean result = userAddressService.updateAddress(address);
            if (result) {
                return ResponseResult.okResult(true);
            } else {
                return ResponseResult.errorResult(500, "更新地址失败");
            }
        } catch (IllegalArgumentException e) {
            return ResponseResult.errorResult(400, e.getMessage());
        } catch (Exception e) {
            return ResponseResult.errorResult(500, "更新地址失败：" + e.getMessage());
        }
    }

    /**
     * 删除地址
     * @param addressId 地址ID
     * @param userId 用户ID
     * @return 操作结果
     */
    @DeleteMapping("/delete")
    public ResponseResult<Boolean> deleteAddress(@RequestParam("addressId") Long addressId,
                                              @RequestParam("userId") Long userId) {
        boolean result = userAddressService.deleteAddress(addressId, userId);
        if (result) {
            return ResponseResult.okResult(true);
        } else {
            return ResponseResult.errorResult(500, "删除地址失败");
        }
    }

    /**
     * 设置默认地址
     * @param addressId 地址ID
     * @param userId 用户ID
     * @return 操作结果
     */
    @PutMapping("/default")
    public ResponseResult<Boolean> setDefaultAddress(@RequestParam("addressId") Long addressId, @RequestParam("userId") Long userId) {
        boolean result = userAddressService.setDefaultAddress(addressId, userId);
        if (result) {
            return ResponseResult.okResult(true);
        } else {
            return ResponseResult.errorResult(500, "设置默认地址失败");
        }
    }
}
